package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youan.pangu.base.bean.ElementTree;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.dao.domain.UserRole;
import com.youan.serve.service.IFunctionService;
import com.youan.serve.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @description:
 * @author: 辛晟昊
 * @time: 2020/2/26 15:25
 */
@Controller
public class LoginController {
    @Autowired
    private IFunctionService functionService;
    @Autowired
    private IUserRoleService userRoleService;

    /*@PostMapping("/login")
    public Map<String, Object> login(String userName, String password) throws Exception {
        Map result = new HashMap();
        // 验证用户，返回token
        result.put("token",operate.login(userName, password));
        // 获取用户的详细信息，菜单权限
        result.put("routers",functionService.getFunctionsElementTree());
        return result;
    }*/

    /**
     * 界面刷新
     * @param userId
     */
    @PostMapping("/refresh")
    public Map<String, Object> refresh(String userId){
        Map<String, Object> result = new HashMap<String, Object>();
        QueryWrapper<UserRole> wrapper = new QueryWrapper<UserRole>();
        List<UserRole> roleUserList = userRoleService.list(wrapper.eq("user_id",userId));
        List<String> roleIdList = new ArrayList<String>();
        if(roleUserList.size()<=0) {
            result.put("routers",new ArrayList<ElementTree>());
        }else {
            for(int i=0; i<roleUserList.size(); i++) {
                roleIdList.add(roleUserList.get(i).getRoleId());
            }
            result.put("routers",functionService.getFunctionsElementTreeByUserRoles(roleIdList));
        }
        return result;
    }
}
